import React from "react";
import { Board } from "./Board";
import { NumberOrNull, StringOrNull } from "./Types";

interface IProps {
  //
}

interface IState {
  history: Array<history>;
  stepNumber: number;
  slelectHistory: NumberOrNull;
  xIsNext: boolean;
  isDescending: boolean;
}

interface history {
  squares: Array<StringOrNull>;
  rowNumber: NumberOrNull;
  colNumber: NumberOrNull;
}

function calculateWinner(squares: Array<StringOrNull>) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      return squares[a];
    }
  }
  return null;
}

export default class Game extends React.Component<IProps, IState> {
  constructor(props: IProps) {
    super(props);
    this.state = {
      history: [
        {
          squares: Array(9).fill(null),
          rowNumber: null,
          colNumber: null,
        },
      ],
      isDescending: false,
      stepNumber: 0,
      slelectHistory: null,
      xIsNext: true,
    };
  }

  handleClick(i: number) {
    const history = this.state.isDescending
      ? this.state.history.slice(
          this.state.stepNumber,
          this.state.history.length
        )
      : this.state.history.slice(0, this.state.stepNumber + 1);
    const current = history[this.state.stepNumber];
    const squares = current.squares.slice();

    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.state.xIsNext ? "X" : "O";
    const newhistory: history[] = [
      {
        squares: squares,
        rowNumber: Math.floor(i / 3) + 1,
        colNumber: (i % 3) + 1,
      },
    ];

    this.setState({
      history: this.state.isDescending
        ? newhistory.concat(history)
        : history.concat(newhistory),
      stepNumber: this.state.isDescending ? 0 : history.length,
      slelectHistory: null,
      xIsNext: !this.state.xIsNext,
    });
  }

  jumpTo(step: number) {
    this.setState({
      stepNumber: step,
      slelectHistory: step,
      xIsNext: step % 2 === 0,
    });
  }

  render() {
    const history = this.state.history;
    const current = history[this.state.stepNumber];
    const winner = calculateWinner(current.squares);
    let status;
    if (winner) {
      status = "Winner: " + winner;
    } else {
      status = "Next player: " + (this.state.xIsNext ? "X" : "O");
    }

    const moves = history.map((step, move) => {
      const desc = (
        this.state.isDescending ? this.state.history.length - move - 1 : move
      )
        ? "Go to move #" +
          move +
          "(" +
          step.rowNumber +
          "," +
          step.colNumber +
          ")"
        : "Go to game start";
      return (
        <li key={move}>
          <button
            onClick={() => this.jumpTo(move)}
            className={
              move == this.state.slelectHistory ? "history-selected" : ""
            }
          >
            {desc}
          </button>
        </li>
      );
    });

    return (
      <div className="game">
        <div className="game-board">
          <Board
            squares={current.squares}
            onClick={(i: number) => this.handleClick(i)}
          />
        </div>
        <div className="game-info">
          <div>{status}</div>
          <div>
            <button onClick={() => this.change()}>更改排序</button>
          </div>
          <ol>{moves}</ol>
        </div>
      </div>
    );
  }
  change(): void {
    const history = this.state.history.slice();
    history.reverse();
    this.setState({
      history: history,
      isDescending: !this.state.isDescending,
      stepNumber: history.length - 1 - this.state.stepNumber,
      slelectHistory: history.length - 1 - this.state.slelectHistory!,
    });
  }
}
